package com.atguigu.gmall.cart.async.impl;

import com.atguigu.gmall.cart.async.CartAsyncService;
import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


@Service
public class CartAsyncServiceImpl implements CartAsyncService {

    @Autowired
    private CartInfoMapper cartInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 修改购物车数据
     *
     * @param cartInfo
     */
    @Override
    @Async
    public void updateCartInfo(CartInfo cartInfo) {

        QueryWrapper<CartInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", cartInfo.getUserId());
        wrapper.eq("sku_id", cartInfo.getSkuId());
        cartInfoMapper.update(cartInfo, wrapper);
    }

    /**
     * 添加购物车数据
     *
     * @param cartInfo
     */
    @Override
    @Async
    public void saveCartInfo(CartInfo cartInfo) {

        cartInfoMapper.insert(cartInfo);
    }

    @Override
    @Async
    public void deleteCarInfo(String userTempId) {
        //        删除数据库数据
        QueryWrapper<CartInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userTempId);
        cartInfoMapper.delete(wrapper);
    }

    /**
     * 选中状态变更
     *
     * @param userId
     * @param isChecked
     * @param skuId
     */
    @Override
    @Async
    public void checkCart(String userId, Integer isChecked, Long skuId) {

        UpdateWrapper<CartInfo> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("sku_id", skuId);
        wrapper.set("is_checked", isChecked);
        cartInfoMapper.update(new CartInfo(), wrapper);
    }

    /**
     * 删除购物车某一个
     *
     * @param userId
     * @param skuId
     */
    @Override
    @Async
    public void deleteCartInfo(String userId, Long skuId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
                .eq("user_id", userId).eq("sku_id", skuId));
    }
}
